#!/bin/bash

dmHost=192.168.141.129
dmPort=8080

if [ $1 == "add" ] && [ $2 == "node" ]; then
	nodeName=$3
	ip=$4
	curl "http://$dmHost:$dmPort/addNode?name=$nodeName&ip=$ip"
elif [ $1 == "get" ] && [ $2 == "nodes" ]; then
	curl "http://$dmHost:$dmPort/getNodes"
elif [ $1 == "get" ] && [ $2 == "containers" ]; then
	curl "http://$dmHost:$dmPort/getContainers"
elif [ $1 == "get" ] && [ $2 == "deploys" ]; then
	curl "http://$dmHost:$dmPort/getDeployments"
elif [ $1 == "scale" ]; then
	serviceName=$2
	replicas=$3
	curl "http://$dmHost:$dmPort/scaleDeployment?name=$serviceName&mode=w&replicas=$replicas"
elif [ $1 == "upgrade" ] && [ $2 == "w" ]; then
	serviceName=$3
	image=$4
	curl "http://$dmHost:$dmPort/upgradeDeployment?name=$serviceName&mode=w&workerImage=$image"
elif [ $1 == "backup" ]; then
	serviceName=$2
	curl "http://$dmHost:$dmPort/backup?name=$serviceName"
elif [ $1 == "deploy" ] && [ $2 == "w" ]; then
	proto=$3
	cata=$4
	serviceName=$5
	containerPort=$6
	hostPort=$7
	image=$8
	replicas=$9
	curl "http://$dmHost:$dmPort/addDeployment?proto=$proto&catalog=$cata&name=$serviceName&mode=$mode&containerPort=$containerPort&hostPort=$hostPort&image=$image&replicas=$replicas"
elif [ $1 == "deploy" ] && [ $2 == "redis-sentinel" ]; then
	serviceName=$3
        containerPort=$4
        hostPort=$5
        image=$6
        replicas=$7
        curl "http://$dmHost:$dmPort/addDeployment?name=$serviceName&catalog=redis-sentinel&containerPort=$containerPort&hostPort=$hostPort&image=$image&replicas=$replicas"
elif [ $1 == "deploy" ] && [ $2 == "mysql" ]; then
	serviceName=$3
	containerPort=3306
	hostPort=$5
	image=$6
	replicas=$7
	curl "http://$dmHost:$dmPort/addDeployment?name=$serviceName&catalog=mysql&containerPort=$containerPort&hostPort=$hostPort&image=$image&replicas=$replicas"
elif [ $1 == "log" ]; then
	name=$2
	curl "http://$dmHost:$dmPort/getLog?name=$name"
elif [ $1 == "help" ]; then
	echo "dmctl log [nodeName | serviceName]"
	echo "dmctl add node [name] [ip]"
	echo "dmctl get nodes"
	echo "dmctl get containers"
	echo "dmctl get deploys"
	echo "dmctl scale [serviceName] [replicas]"
	echo "dmctl backup [serviceName]"
	echo "dmctl upgrade w [serviceName] [image]"
	echo "dmctl deploy w http normal [serviceName] [containerPort] [hostPort] [image] [replicas]"
	echo "dmctl deploy redis-sentinel [serviceName] [containerPort] [hostPort] [image] [1,slaveReplicas]"
	echo "dmctl deploy mysql [serviceName] [containerPort] [hostPort] [image] [1,slaveReplicas]"
fi
